#include "map.h" 
using namespace std;  
  
//数据的插入--第一种：用insert函数插入pair数据 
void  map_test_insert1()  
{  
    //用insert函数插入pair数据
    map<int, string> mapStudent;  
    mapStudent.insert(pair<int, string>(1, "student_one"));  
    mapStudent.insert(pair<int, string>(2, "student_two"));  
    mapStudent.insert(pair<int, string>(3, "student_three")); 

    cout << " print map1 content: " << endl;
    map<int, string>::iterator iter;  
    for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)  
       cout << iter->first << ' ' << iter->second << endl; 
    cout << endl;
    cout << endl; 
}

//数据的插入--第二种：用insert函数插入value_type数据
void map_test_insert2()    
{    
    //用insert函数插入value_type数据
    map<int, string> mapStudent;    
    mapStudent.insert(map<int, string>::value_type (11, "student_one"));    
    mapStudent.insert(map<int, string>::value_type (12, "student_two"));    
    mapStudent.insert(map<int, string>::value_type (13, "student_three"));

        cout << " print map2 content: " << endl;
    map<int, string>::iterator iter;    
    for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)  
        cout << iter->first << ' ' << iter->second << endl;  
    cout << endl;
    cout << endl;    
}

// 用insert函数进行多个插入 
void map_test_insert3()  
{
    std::map<char, int> map1;
 
    // 插入单个值
    map1.insert(std::pair<char, int>('a', 100));
    map1.insert(std::pair<char, int>('z', 200));
 
    //指定位置插入
    std::map<char, int>::iterator it = map1.begin();
    map1.insert(it, std::pair<char, int>('b', 300));  //效率更高
    map1.insert(it, std::pair<char, int>('c', 400));  //效率非最高

    cout << " print map3 content: " << endl;
    map<char, int>::iterator iter;    
    for(iter = map1.begin(); iter != map1.end(); iter++)  
        cout<<iter->first<<' '<<iter->second<<endl; 
    cout << endl;
    cout << endl;
}

//数据的插入--第三种：用数组方式插入数据
void map_test_insert4()   
{    
    //用数组方式插入数据
    map<int, string> mapStudent;    
    mapStudent[1] = "student_one";   
    mapStudent[2] = "student_two";    
    mapStudent[3] = "student_three"; 

    cout << " print map4 content: " << endl;
    map<int, string>::iterator iter;    
    for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)    
        cout << iter->first << ' ' << iter->second << endl;  
    cout << endl;
    cout << endl;  
}

void map_test_find() 
{
    map<int, string> map1;
    map1.insert(map<int, string>::value_type (1, "one"));    
    map1.insert(map<int, string>::value_type (2, "two"));    
    map1.insert(map<int, string>::value_type (3, "three"));
    map1.insert(map<int, string>::value_type (4, "four"));
    map1.insert(map<int, string>::value_type (5, "five"));
    map<int, string>::iterator itMap = map1.find(6);

    cout << " test map find: " << endl;
    if (itMap != map1.end()) {  
        std::cout << "Found key with value " << itMap->second << endl;  
    } else {  
        std::cout << "Key not found." << endl;  
    }  
    cout << endl;
    cout << endl;
} 

